package homework;

import java.util.Stack;

/**
 * push是O(1),pop是O(n)
 * @author noob
 * @version 1.0
 * @date 2021/3/15 7:22
 */
public class MyQueue2 {

    private Stack<Integer> stack;
    int front; //追踪存储队首的元素

    public void push(int x) {
        if(stack.isEmpty()) front = x;
        stack.push(x);
    }

    public int peek() {
        return front;
    }


    public int pop() {

        Stack<Integer> stack2 = new Stack<>();

        while(stack.size() > 1) {
            front = stack.peek();
            stack2.push(stack.pop());
        }

        int ret = stack.pop();

        while(!stack2.isEmpty())
            stack.push(stack2.pop());

        return ret;
    }

    public boolean isEmpty() {
        return stack.isEmpty();
    }
}
